package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.HolderAdapter;
import com.nest.WorkNest.adapter.HolderAdapter.ViewCreator;
import com.nest.WorkNest.api.XutilsPost;
import com.nest.WorkNest.tools.Tool;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AddressManagerAct extends BaseActivity implements OnItemClickListener, OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private Context context = AddressManagerAct.this;
    private HolderAdapter<HashMap<String, String>> mAdapter;
    private List<HashMap<String, String>> data = new ArrayList<>();
    private ListView lv_addressmanager;
    private SwipeRefreshLayout mSwipeLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_addressmanager);
        initBar();
        actionbar_side_name.setVisibility(View.VISIBLE);
        actionbar_side_name.setText("管理收货地址");
        ll_tv_actionbar_side_right.setVisibility(View.VISIBLE);
        tv_actionbar_side_right.setText("新增");
        ll_iv_actionbar_side_left.setVisibility(View.VISIBLE);
        back();
        initView();

    }

    public void initView() {
        lv_addressmanager = (ListView) findViewById(R.id.lv_addressmanager_list);
        lv_addressmanager.setOnItemClickListener(this);
        lv_addressmanager.setSelector(R.color.bg_transparent);
        ll_tv_actionbar_side_right.setOnClickListener(this);
        createAdapter();
        loadAddressList();
        mAdapter.update(data);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.rl_modulename_refresh);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

    }

    public void loadAddressList() {

        XutilsPost xutilsPost = new XutilsPost();
        XutilsPost.IOAuthCallBack ioAuthCallBack = new XutilsPost.IOAuthCallBack() {
            @Override
            public void getIOAuthCallBack(String result) {
                JSONObject jsonObject = Tool.getJsonObject(result);
                JSONArray jsonArray;
                if (Tool.getString(jsonObject, "resultCode").equals("0")) {
                    jsonArray = Tool.getJsonArray(result, "dataList");
                    data = Tool.getListMapByJsonArray(jsonArray);
                    mAdapter.update(data);
                } else if (Tool.getString(result, "resultCode").equals("9001")) {
                    showToast("token已过期，请重新登录");
                    Intent intent = new Intent(context, LoginAct.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void getDealException(HttpException httpException, String result) {

            }

            @Override
            public void getLoadingCallBack(long total, long current, boolean isUploading) {

            }
        };
        xutilsPost.doPostAddressList(context, "", ioAuthCallBack);
    }


    public void createAdapter() {
        ViewCreator<HashMap<String, String>> creator = new ViewCreator<HashMap<String, String>>() {

            @Override
            public View createView(LayoutInflater inflater, int position,
                                   HashMap<String, String> map) {

                return LayoutInflater.from(context).inflate(R.layout.items_listview_addressmanager,
                        null);
            }

            @Override
            public void updateView(View view, final int position, HashMap<String, String> map) {
                // ImageView iv_myproduct_pic = (ImageView) view
                // .findViewById(R.id.iv_myproduct_pic);
                TextView tv_addressmanager_name = (TextView) view
                        .findViewById(R.id.tv_addressmanager_name);
                TextView tv_addressmanager_address = (TextView) view
                        .findViewById(R.id.tv_addressmanager_address);
                TextView tv_addressmanager_telphone = (TextView) view
                        .findViewById(R.id.tv_addressmanager_telphone);
                RadioButton rdoBtn_addressmanager_select = (RadioButton) view
                        .findViewById(R.id.rdoBtn_addressmanager_select);
                ImageView iv_addressmanager_default = (ImageView) view
                        .findViewById(R.id.iv_addressmanager_default);
                rdoBtn_addressmanager_select.setClickable(false);
                tv_addressmanager_name.setText(map.get("name"));
                tv_addressmanager_address.setText(map.get("address"));
                tv_addressmanager_telphone.setText(map.get("cellPhone"));

            }
        };
        mAdapter = new HolderAdapter<>(this.getLayoutInflater(), creator);
        lv_addressmanager.setAdapter(mAdapter);
        mAdapter.update(data);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Intent intent = new Intent(context, AddAddressAct.class);
        intent.putExtra("addressId", data.get(position).get("addressId"));
        intent.putExtra("name", data.get(position).get("name"));
        intent.putExtra("cellPhone", data.get(position).get("cellPhone"));
        intent.putExtra("address", data.get(position).get("address"));
        intent.putExtra("province", data.get(position).get("province"));
        intent.putExtra("city", data.get(position).get("city"));
        intent.putExtra("postCard", data.get(position).get("postCard"));
        startActivity(intent);
        mAdapter.update(data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_tv_actionbar_side_right:
                startActivity(new Intent(context, AddAddressAct.class));
                break;
        }
    }

    @Override
    protected void onResume() {
        loadAddressList();
        super.onResume();
    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeLayout.setRefreshing(false);
                loadAddressList();
            }
        }, 2000);
    }
}
